﻿using System.Linq;
using System.Web.Mvc;
using Omu.Awesome.Mvc;
using Omu.AwesomeDemo.Core.Model;
using Omu.AwesomeDemo.Core.Repository;

namespace Omu.AwesomeDemo.WebUI.Controllers
{
    public class HobbyAutocompleteController : Controller
    {
        private readonly IRepo<Hobby> repo;

        public HobbyAutocompleteController(IRepo<Hobby> repo)
        {
            this.repo = repo;
        }

        public ActionResult Search(string searchText, int maxResults)
        {
            return Json(repo.GetAll().Where(
                o => o.Name.ToLower().Contains(searchText.ToLower()))
                .Take(maxResults)
                .Select(v => new IdTextItem { Id = v.Id, Text = v.Name }));
        }
    }
}